﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Lyon
{
    /// <summary>
    /// Xml文件配置。
    /// </summary>
    public class XmlConfig
    {
        /// <summary>
        /// 命名空间前缀
        /// </summary>
        static public string Prefix = "Lyon";
        /// <summary>
        /// 命名空间Uri
        /// </summary>
        static public string Uri = "http://www.lyon.cn/XmlConfig.xsd";

        XmlDocument document = new XmlDocument();
        XmlNamespaceManager _NamespaceManager;
        /// <summary>
        /// 获取给定类型的SessionName。
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        static public string GetSectionName(Type type)
        {
            string sectionName = type.FullName;
            sectionName = sectionName.Replace("+", "/");
            string prefix = String.Format("/{0}:", XmlConfig.Prefix);
            sectionName = "*" + prefix + sectionName.Replace("/", prefix);
            return sectionName;
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="fileName"></param>
        public XmlConfig(string fileName)
        {
            document.Load(fileName);
            XmlNamespaceManager nsmanager = new XmlNamespaceManager(document.NameTable);
            nsmanager.AddNamespace(Prefix, Uri);
            _NamespaceManager = nsmanager;
        }
        /// <summary>
        /// 获取给定类型的Section实例。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetConfigSection<T>() where T : ConfigSection, new()
        {
            string sectionName = GetSectionName(typeof(T));
            XmlNode sectionNode = document.SelectSingleNode(sectionName, _NamespaceManager);
            if (sectionNode == null)
                return null;
            T section = new T();
            section.Load(sectionNode);
            return section;
        }

        static XmlConfig _Current;
        /// <summary>
        /// 获取当前配置文件
        /// </summary>
        public static XmlConfig Current
        {
            get { return _Current; }
        }
        /// <summary>
        /// 启用当前配置
        /// </summary>
        /// <param name="fileName"></param>
        public static void Config(string fileName)
        {
            _Current = new XmlConfig(fileName);
        }
    }
    /// <summary>
    /// 配置节点。
    /// </summary>
    abstract public class ConfigSection
    {
        Dictionary<string,string> table = new Dictionary<string,string>();
        /// <summary>
        /// 初始化
        /// </summary>
        protected ConfigSection()
        {
        }
        /// <summary>
        /// 获取指定名称的值。
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        protected string this[string name]
        {
            get
            {
                return table[name];
            }
        }

        internal void Load(XmlNode node)
        {
            table.Clear();
            foreach (XmlNode attr in node.Attributes)
            {
                table.Add(attr.Name, attr.Value);
            }
            //foreach (XmlNode childNode in node.ChildNodes)
            //{
            //    table.Add(childNode.Name, childNode.InnerText);
            //}
        }
    }
}
